﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Speech;
using System.Speech.Synthesis;
using System.IO;

namespace GuidingRobot
{
    public class RobotSpeaking : IRobotSpeaking
    {
        private string str;     // str 存储文本内容
        private string file;    // file为文本文件路径


        /**
         * 私有构造函数
         */
        private RobotSpeaking() { }


        /**
         * 构造函数
         * @param string ：存储展板说明的文本文件路径
         */
        public RobotSpeaking(string s)
        {
            this.file = s;
            if (!File.Exists(file))
            {
                FileStream fs = File.Create(file);
            }

            this.str = File.ReadAllText(file, Encoding.Default);
        }


        /**
         * GetBoardExplanation函数返回对应展板的宣传内容
         * @param int : 展板号
         * @return string ：展板宣传内容 
         */
        private string GetBoardExplanation(int i)
        {
            string str1 = this.str;
            string[] str2 = new string[str1.Length];
            str2 = str1.Split(new char[] { '$' });
            char[] s = "123456789".ToCharArray();

            string[] str3 = Array.FindAll(str2, FindNoBlankString);

            if (i > str3.Length)
            {
                return "";
            }
            else
            {
                str3[i - 1] = str3[i - 1].Trim(s);
                str3[i - 1] = str3[i - 1].Trim();
                return str3[i - 1];
            }
        }


        private bool FindNoBlankString(string s)
        {
            if (s.Trim()!="")
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        public string Speak(int i)
        {
            string s = GetBoardExplanation(i);
            SpeechSynthesizer speaker = new SpeechSynthesizer();
            speaker.SpeakAsync(s);
            //speaker.Speak(s);
            return s;
        }

    }
}
